<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
	<head>
		<#include "../head.html">
		<title>资源管理</title>
		<script type="text/javascript">
		var queryForm,queryDialog,inputForm,inputDialog,grid,operationGrid;
		$(function(){
			//查询
			queryForm = Rainbow.Form({
				  formId:"#queryForm"
				 ,lable:true
				 ,inputArray:[
					 {lable:"名称",name:"name",type:"textbox",condition:"like"}
					,{lable:"类型",name:"type",type:"combobox",condition:"eq",option:{valueField:'code'},code:"resource"}
				  ]
				 ,queryForm:true
			 });
			
			queryDialog = Rainbow.Dialog({
		  		  title:"查询条件"
				 ,dialogId:'#queryDialog'
				 ,form:queryForm
				 ,width: '80%'
			 	 ,height: '50%'
		 		 ,modal: false
			     ,closable:true
			 	 ,buttons:[{
						text:'确认',
						iconCls:'icon-ok',
						handler:function(){
							grid.query();
							queryDialog.dialog('close');
						}
					}]
			 });
			//输入
			inputForm = Rainbow.Form({
				  formId:"#inputForm"
				 ,lable:true
				 ,inputArray:[
					 			 {name:"id",type:"hidden"}
					 			,{name:"parent.id",type:"hidden"}
					 			,{lable:"名称",name:"name",option:{required:true}}
					 			,{lable:"类型",name:"type",type:"combobox",option:{valueField:'code',required:true},code:"resource"}
					 			,{lable:"顺序",name:"sort",type:"numberbox",option:{precision:0,value:1,required:true}}
					 			,{lable:"配置(JSON格式)",name:"config",option:{multiline:true,value:"{}"},style:{width:"700px",height:"50px"}}
	 						]
			 	 ,colNum:3
			 });
			 inputDialog = Rainbow.Dialog({
				  title:"资源"
				 ,width:800
				 ,height:500
				 ,onlyLeafCheck:true
				 ,dialogId:'#inputDialog'
				 ,form:inputForm
				 ,serviceName:"ResourceService"
				 ,methodName:"save"
				 ,callback:function(result){
					 var data = result.data;
					 $.extend(data,JSON.parse(data.config));
					 var parentId;
					 if(data.parent){
						 parentId = data.parent.id
					 }
					 var node = grid.treegrid('find',data.id);
					 if(!node){
						grid.treegrid('append',{
							parent: parentId
						   ,data:[data]
						});
					 }else{
						grid.treegrid('update',{
							 id: data.id
							,row: data
						});
					 }
					 
				 }
			     ,beforeSendData:function(data){
			    	 if(!data.id){
			    		 var row = grid.treegrid("getSelected");
				    	 if(row){
				    		 data.parent = {id:row.id};
				    	 }
			    	 }
			    	 var operations = operationGrid.datagrid("getRows");
			    	 data["operations"] = operations;
			     }
			     ,onConfirm:function(){
			    	 operationGrid.acceptChanges();
				 }
			 });
			
			//表格
			 grid = new Rainbow.TreeGrid({
				 treeField:"name"
				,queryForm:queryForm
				,serviceName:"ResourceService"
				,methodName:"query"
				,paths:[""]
				,beforeLoad:function(filterOption){
					 filterOption.order=[{sort:1}]
				}
			 	,afterLoad:function(result){
			 		if(result.data){
			 			var data = result.data;
			 			for(i=0;i<data.length;i++){
			 				category = data[i];
			 				$.extend(category,JSON.parse(category.config));
			 				if(category.children){
			 					for(j=0;j<category.children.length;j++){
			 						menu = category.children[j];
			 						$.extend(menu,JSON.parse(menu.config));
			 						if(menu.children){
			 							for(k=0;k<menu.children.length;k++){
			 								button = menu.children[k];
			 								$.extend(button,JSON.parse(button.config));
			 							}
			 						}
			 					}
			 				}
			 			}
			 		}
			 	}
				,columns: [[
			         {field:'name',title:'名称',width:150}
			        ,{field:'type',title:'类型',width:100
			        	,formatter: function(value,row,index){
			        		return CodeCache.getNameFromList("resource",value);
						}
			        }
			        ,{field:'config',title:'配置',width:300}
			        ,{field:'sort',title:'顺序',width:100}
				]]
				,onDblClickRow: function (row) {
					grid.treegrid('select',row.id);
					grid.update(inputDialog,inputForm,["operations"]);
			    }
				,onSave(){
			    	var row = grid.treegrid("getSelected");
			    	var data = {};
			    	var child;
			    	if(row){
			    		child = row.children;
			    	}else{
			    		child = grid.treegrid("getRoots");
			    	}
			    	if(child && child.length>0){
	    				data.sort = parseInt(child[child.length-1].sort) + 1;
		    		}else{
		    			data.sort = 1;
		    		}
			    	inputForm.setData(data);
			    	operationGrid.clear();
			    }
				,onUpdate:function(data){
					if(data.operations){
						operationGrid.loadData(data.operations);
					}else{
						operationGrid.loadData([]);
					}
				}

			});
			
			var serviceunitList= ServiceUnit.queryServiceUnit();
			operationGrid = new Rainbow.Grid({
				 gridId:"#operationGrid"
				,title:"操作配置"
				,toolbar:"#operationGridToolbar"
				,pagination:false
				,loadDate:false
				,rowEditEnable:true
				,rownumbers:true
				,deleteMode:"local"
				,width:"100%"
				,height:"280"
				,border:false
				,columns:[[
					 {field:'id',title:'选择',checkbox:true}
					,{field:'code',title:'编码',width:200,editor:{type:'combotreegrid',options:Rainbow.inputOption.combotreegrid({
								 idField:"id"
								,textField:'id'
								,treeField:'name'
								,panelWidth:600
								,panelHeight:200
								,columns: 
									[[
										 {field:'name',title:'名称',width:150}
										,{field:'describe',title:'描述',width:150}
				 				    ]]
								,loader:function(param,success){
									success(serviceunitList);
								}
	 							,onBeforeSelect:function(node){
	 								return !node.children
								}
								,onSelect:function(row){
									var codeEditor = operationGrid.datagrid("getEditor",{index:operationGrid.editIndex,field:"code"});
									var serviceUnit = $(codeEditor.target).combotreegrid("grid").treegrid("getParent",row.id);
									var nameEditor = operationGrid.datagrid("getEditor",{index:operationGrid.editIndex,field:"name"});
									$(nameEditor.target).textbox("setValue",serviceUnit.describe+"_"+row.describe);
								}
								,required:true
							})
						}
					 }
					,{field:'name',title:'名称',width:200,editor:{type:'textbox'/* ,options:{required:true} */}}
				]]
			});
			
		});
		
		function copy(){
			var target = grid.treegrid("getSelected");
			var source = grid.treegrid("getCheckedNodes");
			var parentId;
			if(source.length>0){
				for(i=0;i<source.length;i++){
					if(target != null){
						parentId = target.id;
						source[i].parent = {"id":target.id};
					}else{
						delete source[i].parent;
					}
					source[i].checked=false;
				}
			    var option = {
		                "serviceName":"ResourceService"
		               ,"methodName":"batchSave"
		               ,"params":[source]
		               ,"async":true
		               ,"callback":function(result){
		            	   grid.treegrid('append',{
								parent: parentId
							   ,data:result.data
							});
		               }
		           };
				ServiceUnit.request(option);
			}
		}
		
		</script>
	</head>
	<body class="easyui-layout" data-options="fit:true">
		<div id="toolbar"></div>
		<div id="treeGrid"></div>
		
		<div id="queryDialog">
			<div id="queryForm"></div>
		</div>
		
		<div id="inputDialog">
			<form id="inputForm"></form>
			<div id="operationGridToolbar">
				<a href="#" class="easyui-linkbutton" iconCls="icon-add" onclick="operationGrid.appendRow({});" plain="true">添加</a>
		        <a href="#" class="easyui-linkbutton" iconCls="icon-remove" onclick="operationGrid.delete();" plain="true">删除</a>
			</div>
			<div id="operationGrid"></div>
		</div>
	</body>
	<script type="text/javascript">
		Rainbow.Util.initButton("#toolbar");
	</script>
</html>